queue
A queue is a line at a shop. New people join at the back, and the person at the front is served first. You push an item onto the back of a queue and you pop the item off the front, so the item that has waited longest is always next. That rule is called first in, first out, and it is the opposite of a stack, where the newest item comes out first.
Like a stack, a queue gives you only its ends: the back to add to and the front to take from. You cannot ask for the item in the middle.
Reach for a queue when things should be handled in the order they arrived:
/// @type {queue<int>}
/// @capacity 8
var turn_order = [];/// @type says every item is an int. /// @capacity says the most items the queue can hold, and leaving it off gives you 8. Like every container on the GBA, its space is set aside when the game is built, not grown while it runs.
The same queue with no comment: var turn_order = queue_create(8, 0); (an empty queue of whole numbers that holds 8) or var turn_order: queue<int, 8> = [];. See queue_create. All three are the same queue, and a queue given no size by any of them holds 8.
A starting list in brackets puts the first item in the brackets at the front: [1, 2, 3] starts with 1 next in line.
A queue has one rule the list and stack do not: its capacity must be a power of two. That means 2, 4, 8, 16, 32, and so on - never 10 or 20. The rule comes from the Game Boy Advance module's container a queue is built on, which refuses any other size. The default of 8 already follows it, so it only comes up when you write your own /// @capacity. Pick a number that is not a power of two and the project is refused before the game runs, with a message that names the next size up:
`/// @capacity 10` is not a power of two - a queue<T> always needs oneMap and set have the same rule.
| Function | What it does | It gives back |
|---|---|---|
queue_push(queue, value) | joins the back of the line | true if it fit, false if the queue was full |
queue_pop(queue) | removes the front item | that item |
queue_peek(queue) | looks at the front item without removing it | that item |
queue_empty(queue) | asks whether there is nothing in it | true or false |
queue_contains(queue, value) | asks whether value is anywhere in it, without taking anything off | true or false |
queue_length(queue) | counts the items | the count |
queue_clear(queue) | empties it | nothing |
queue_copy(queue) | makes a second queue just like it, with the same room | the new queue |
Push and pop each take the same short time however long the line is. That is the reason to use a queue instead of a list for a line of waiting things: removing the first item of a list shifts every other item down one place, while popping a queue moves nothing.
An empty queue does not crash the game either. queue_pop and queue_peek hand back a plain default value - 0, 0.0, false, an empty piece of text, or nothing for an instance reference
0 could also be a real item, so askfirst with queue_empty whenever the difference matters. queue_push on a full queue adds nothing and returns false; it never drops the item at the front to make room.
Three fighters wait for their turn, and one more joins:
/// @type {queue<int>}
/// @capacity 4
var turn_order = [1, 2, 3];
queue_push(turn_order, 4); // joins the back - the queue is now full
var full = queue_push(turn_order, 5); // false - no room, nothing changed
var next = queue_pop(turn_order); // 1 - the front of the line
var after = queue_peek(turn_order); // 2 - look, but leave it thereA waiting item is often more than one number: which enemy to spawn, and after how many frames. A struct holds those together. The queue has to be shown what the struct looks like once, so it starts with one real item that is popped straight back off:
/// @type {queue<struct Job>}
var jobs = [ { kind: 1, delay: 0 } ];
var seed = queue_pop(jobs); // now empty - the shape is learned
queue_push(jobs, { kind: 2, delay: 30 });
var next_job = queue_pop(jobs);
var wait = next_job.delay; // 30[i]A queue cannot be indexed: turn_order[1] is refused when the project is checked. The whole guarantee of a queue is that items leave in the order they arrived, and reaching into the middle would break it. Accessors covers this across all the containers.
Reach for a queue when the item that has waited longest should go first. If the newest should go first, that is a stack. If you need to search, reorder, or remove from the middle, use a list. Data containers compares all of them side by side.